//
//  NSError+Category.m
//  Community
//
//  Created by xtjun on 2023/7/4.
//

#import "NSError+Category.h"

@implementation NSError (Category)
 
+ (instancetype)errorWithCode:(NSInteger)code {
    return [self errorWithCode:code errorReason:nil];
}

+ (instancetype)errorWithCode:(NSInteger)code errorReason:(nullable NSString *)errorReason {
    NSDictionary *userInfo = nil;
    if (errorReason) {
        userInfo = @{NSLocalizedFailureReasonErrorKey:errorReason};
    }
    
    return [self errorWithCode:code userInfo:userInfo];
}

+ (instancetype)errorWithCode:(NSInteger)code userInfo:(nullable NSDictionary *)userInfo {
    return [NSError errorWithDomain:ERRPRDOMAIN code:code userInfo:userInfo];
}

- (nullable NSDictionary *)getAFNOperationFailingURLResponseData {
    NSData *responseData = [self.userInfo valueForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
    NSDictionary *responseObject = responseData.mj_JSONObject;
    if (responseData.length && [responseObject isKindOfClass:NSDictionary.class]) {
        return responseObject;
    }
    return nil;
}


@end
